package rua.jose.macamo;

import java.text.DecimalFormat;

import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.TextView;

/**
 * LocListener implements the LocationListener interface and keeps track
 * of the users location.
 * @author Panu Parviainen
 *
 */

public class LocListener implements LocationListener {
	protected TextView textView; 
	protected ItemizedOverlayArray ioa;
	protected float angle;
	protected Location location = null;
	protected Location destination = null;
	protected DecimalFormat tooDecimal = new DecimalFormat("0.00");
	protected boolean showDistance = false;
	protected double distance = 0;

	/**
	 * Constructor
	 * @param main Main activity
	 */
	public LocListener(Main main) {
		this.textView = (TextView) main.findViewById(R.id.distance);
		this.ioa = main.items;
	}

	/**
	 * Calculates the distance to the new location
	 * 
	 * @param loc The new location
	 */
	public void onLocationChanged(Location loc) {
	   if(destination != null){
	      distance = calculateDistanceTo(loc, destination);
         textView.setText("The distance is: "+tooDecimal.format(distance));
	   }
	   location = loc;
	}
		
	/**
	 * Calculates the distances for all currently visible locations
	 * @param MyLocation Users location
	 */
	public void distances(Location MyLocation){  
	   double[] distances = new double[ioa.size()];
	   int index = 0;
	   for (mOverlayItem i : ioa.currentlyVisible) {   
	      distances[index] = calculateDistanceTo(MyLocation, mOverlayItemToLocation(i));
	      index++;
	   }
	}
	
	/**
	 * Calculates users distance to a preset location using calculateDistanceTo method
	 * and show the distance.
	 * 
	 * @see calculateDistanceTo(Location me, Location destination)
	 */
	public void showDistance(){
	   if(location != null){
	      distance = calculateDistanceTo(location, destination);
         textView.setText("The distance is: "+tooDecimal.format(distance));
	   }
	}
	
	/**
	 * Wrapper for Locations distanceTo method
	 * @param me Users location
	 * @param destination Users destination
	 * @return @see Location.distanceTo(Location destination) 
	 */
	public double calculateDistanceTo(Location me, Location destination){	
		return me.distanceTo(destination);
	}
	
	/**
	 * Sets users destionation
	 * @param item The mOverlayItem which is the new destination
	 */
	public void setDestination(mOverlayItem item){
	   destination = new Location("destination");
	   destination.setLatitude(item.getPoint().getLatitudeE6()/1E6);
      destination.setLongitude(item.getPoint().getLongitudeE6()/1E6);
	}

	/**
	 * Creates a Location from mOverlayItem
	 * @param m mOverlayItem from which the new Location will be created
	 * @return Location created from the mOverlayItem
	 */
	public Location mOverlayItemToLocation(mOverlayItem m){
	   Location loc = new Location("location2");
      loc.setLatitude(m.getPoint().getLatitudeE6()/1E6);
      loc.setLongitude(m.getPoint().getLongitudeE6()/1E6);
      return loc;
	}
	
	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub	
	}

}
